package cn.xiaobai.spring.boot.utils;

import cn.xiaobai.spring.boot.annotaion.OnMessage;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author yangdaji
 * @Date 2023/11/6
 * @PackageName cn.xiaobai.utils
 * @ClassName ProxyUtils
 */
public class ProxyUtils {

    /**
     * 代理对象绑定频道
     *
     * @param proxy
     * @param topic
     * @param topicMessagesMap
     */
    public static void bindProxy(Object proxy, String topic, Map<String, Map<Object, Method>> topicMessagesMap) {
        // 反射获取目标代理类中的注解方法, 并进行赋值
        Method[] declaredMethods = proxy.getClass().getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            OnMessage onMessage = declaredMethod.getAnnotation(OnMessage.class);
            if (onMessage != null) {
                HashMap<Object, Method> value = new HashMap<>();
                value.put(proxy, declaredMethod);
                topicMessagesMap.put(topic, value);
            }
        }
    }
}
